package com.qf.es.index;

import com.qf.es.utils.ESClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.json.JsonXContent;

import java.io.IOException;

public class IndexService {

    public boolean isExist() throws IOException {
        RestHighLevelClient highLevelClient = ESClient.getClient();
        GetIndexRequest request=new GetIndexRequest("tcy-logs-index");
        boolean exists = highLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        return exists;
    }

    public void createIndex() throws IOException {
        RestHighLevelClient highLevelClient = ESClient.getClient();
        CreateIndexRequest createIndexRequest=new CreateIndexRequest("tcy-logs-index");
        createIndexRequest.settings(
                Settings.builder()
                        .put("number_of_shards",5)
                        .put("number_of_replicas",1)
                        .build())
                        .mapping(
                        JsonXContent.contentBuilder()
                                .startObject() //{
                                .startObject("properties")//"properties":{
                                .startObject("corpName").field("type","keyword").endObject()
                                .startObject("createDate").field("type","date").field("format","yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
                                .startObject("fee").field("type","integer").endObject()
                                .startObject("ipAddr").field("type","ip").endObject()
                                .startObject("longCode").field("type","keyword").endObject()
                                .startObject("mobile").field("type","keyword").endObject()
                                .startObject("operatorId").field("type","integer").endObject()
                                .startObject("province").field("type","keyword").endObject()
                                .startObject("replyTotal").field("type","integer").endObject()
                                .startObject("state").field("type","integer").endObject()
                                .startObject("sendDate").field("type","date").field("format","yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
                                .startObject("smsContent").field("type","text").field("analyzer","ik_max_word").endObject()
                                .endObject()
                                .endObject());

        CreateIndexResponse response = highLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        System.err.println(response);


    }
}
